package com.ruibang.glass.portal.service;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.portal.entity.RoleMenuEntity;
import com.ruibang.glass.portal.mapper.RoleMenuMapper;
import io.seata.common.util.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleMenuService extends ServiceImpl<RoleMenuMapper, RoleMenuEntity> implements IService<RoleMenuEntity> {

    /***
     * 保存角色菜单
     * @param entity
     */
    public void saveRoleMenu(RoleMenuEntity entity) {
        baseMapper.saveRoleMenu(entity);
    }

    /***
     * 根据角色Id删除角色菜单信息
     * @param roleId
     */
    public void delByRoleId(String roleId) {
        baseMapper.delByRoleId(roleId);
    }

    public List<String> getRoleByMenuId(String menuId) {
        if (StringUtils.isNotBlank(menuId)) {
            return baseMapper.getRoleByMenuId(menuId);
        }
        return Collections.emptyList();
    }

    public List<String> getByRoleIds(List<String> roleIds) {
        if (CollectionUtils.isNotEmpty(roleIds)) {
            List<RoleMenuEntity> roleMenuEntity = lambdaQuery().in(RoleMenuEntity::getRoleId, roleIds).list();
            if (CollectionUtils.isNotEmpty(roleMenuEntity)) {
                return roleMenuEntity.stream().map(RoleMenuEntity::getMenuId).collect(Collectors.toList());
            }

        }
        return Collections.emptyList();
    }

    public void delByMenuId(String menuId) {
        lambdaUpdate().eq(RoleMenuEntity::getMenuId, menuId).remove();
    }
}
